package com.deyuanyun.pic.ctrl.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.ctrl.domain.EqptCppowerr;
import com.deyuanyun.pic.ctrl.domain.EqptCppowerrExample;
import com.deyuanyun.pic.ctrl.domain.EqptCptestStation;
import com.deyuanyun.pic.ctrl.mapper.EqptCppowerrMapper;
import com.deyuanyun.pic.ctrl.mapper.EqptCptestStationMapper;
import com.deyuanyun.pic.ctrl.service.CpTestStationExpandService;
import com.deyuanyun.pic.settings.domain.pipe.PipeSegment;
import com.deyuanyun.pic.settings.mapper.pipe.PipeSegmentMapper;

@Service
public class CpTestStationExpandServiceImpl implements CpTestStationExpandService {

    private @Resource EqptCptestStationMapper eqptCptestStationMapper;
    private @Resource PipeSegmentMapper pipeSegmentMapper;
    private @Resource EqptCppowerrMapper eqptCppowerrMapper;
    
    @Override
    public EqptCppowerr queryPower(String testStationId) {
        
        EqptCptestStation testStation = eqptCptestStationMapper.selectByPrimaryKey(testStationId);
        Long mileage = testStation.getMileage();
        String pipeLineId = testStation.getPipeLineId();
        //TODO CK：未来、此处整数的绝对距离转换可能存在问题，当阴保测试桩在管段的最后或者最前时，可能因为精度问题查询不到。
        PipeSegment pipeSegment = pipeSegmentMapper.queryPipeSegmentByMileage(pipeLineId, new BigDecimal(mileage).movePointLeft(3));
        Asserts.assertTrue(pipeSegment != null, "未找到阴保测试桩所属的管段");
        
        
        //TODO CK：待处理、这个地方 或许会有一个问题，阴保电源在场站阀室中，是没有所属管道的，现在却给了一个管段名称。难不成导入的时候还可以给多个管段名称？
        EqptCppowerrExample example = new EqptCppowerrExample();
        example.createCriteria().andPipeSegmentIdEqualTo(pipeSegment.getId());
        List<EqptCppowerr> eqptCppowerrList = eqptCppowerrMapper.selectByExample(example);
        if(eqptCppowerrList == null || eqptCppowerrList.isEmpty()){
            return null;
        }
        return eqptCppowerrList.get(0);
    }

}
